package paper.db;

import java.sql.*;

import java.io.PrintWriter;

import java.util.regex.*;
import java.util.HashSet;
import java.util.Random;
import java.util.Iterator;
import java.util.Vector;

import paper.db.DB;
import paper.tags.TagBag;
import paper.tags.Tag;

public class Tags{

    public void addTag(String tag, String paperkey, String userid){
		ResultSet rs=null;
        Statement st;

        try {
            st = DB.connection.createStatement();
            rs = st.executeQuery("INSERT INTO tags (tag, paperkey, userid, isinvisible) values ("+
					DB.q(tag,16)+", "+DB.q(paperkey,16)+", "+DB.q(userid,16)+", " +"false)"); 
        } catch (SQLException e) {
			System.err.println("Can't add a tag. Buggered if I know why.");
        }
    }

	public void hideTag(String tagid){
		ResultSet rs=null;
		Statement st;
		try{
			st=DB.connection.createStatement();
			rs=st.executeQuery("UPDATE tags SET isinvisible=true where tagid="+DB.q(tagid,16));
		}catch (SQLException e){
			System.err.println("Can't set tag invisible!");
		}	
	}

	public void deleteTag(String tagid){
		ResultSet rs=null;
		Statement st;

		try{
			st=DB.connection.createStatement();
			rs=st.executeQuery("DELETE FROM tags where tagid="+DB.q(tagid,16));
			
		} catch(SQLException e){
			System.err.println("Can't delete a tag. Oh well.");
		}	
	}

	public static TagBag getTagBag(){
		ResultSet rs=null;
		Vector myTags=new Vector();
		TagBag myCollection=new TagBag();
		Statement st;
		try{
			st=DB.connection.createStatement();
			rs=st.executeQuery("SELECT tag, tagid, paperkey, userid FROM tags where isprivate=false"); 
			while(rs.next()){
					Tag tempTag=new Tag(rs.getString("tag"), rs.getString("tagid"), rs.getString("paperkey"), rs.getString("userid"));
				myTags.add(tempTag);
			}	
			myCollection=new TagBag(myTags);
		} catch (SQLException e){
			System.err.println("Err?");
		}
		return myCollection;
	}


	public static TagBag getTagBag(String partialTag){
		ResultSet rs=null;
		Vector myTags=new Vector();
		TagBag myCollection=new TagBag();
		Statement st;
		try{
			st=DB.connection.createStatement();
			rs=st.executeQuery("SELECT tag, tagid, paperkey, userid FROM tags WHERE tag like '"+DB.q(partialTag,32)+"'");
			while(rs.next()){
					Tag tempTag=new Tag(rs.getString("tag"), rs.getString("tagid"), rs.getString("paperkey"), rs.getString("userid"));
				myTags.add(tempTag);
			}	
			myCollection=new TagBag(myTags);
		} catch (SQLException e){
			System.err.println("Err?");
		}
		return myCollection;
	}


	
	public static TagBag getPersonalTagBagForPaperkey(String paperkey, String userid){
		ResultSet rs=null;
		Vector myTags=new Vector();
		TagBag myCollection=new TagBag();
		Statement st;
		try{
			st=DB.connection.createStatement();
			rs=st.executeQuery("SELECT tag, tagid, paperkey, userid from tags where userid='"+userid+"' and paperkey='"+DB.q(paperkey,16)+"' and isinvisible=false");
			// and isprivate=false OR userid=thisuserid
			while(rs.next()){
					Tag tempTag=new Tag(rs.getString("tag"), rs.getString("tagid"), rs.getString("paperkey"), rs.getString("userid"));
				myTags.add(tempTag);
			}	
			myCollection=new TagBag(myTags);

		}catch (SQLException e){
			System.err.println("Err..."+e);
		}
		return myCollection;
	}

	public static TagBag getTagBagForPaperkey(String paperkey, String userid){
		ResultSet rs=null;
		Vector myTags=new Vector();
		TagBag myCollection=new TagBag();
		Statement st;
		try{
			st=DB.connection.createStatement();
			rs=st.executeQuery("SELECT tag, tagid, paperkey, userid from tags where paperkey='"+DB.q(paperkey,16)+"' and isinvisible=false");
			// and isprivate=false OR userid=thisuserid
			while(rs.next()){
					Tag tempTag=new Tag(rs.getString("tag"), rs.getString("tagid"), rs.getString("paperkey"), rs.getString("userid"));
				myTags.add(tempTag);
			}	
			myCollection=new TagBag(myTags);

		}catch (SQLException e){
			System.err.println("Err..."+e);
		}
		return myCollection;
	}

	public static TagBag getTagBagForPaperkey(String paperkey){
		ResultSet rs=null;
		Vector myTags=new Vector();
		TagBag myCollection=new TagBag();
		Statement st;
		try{
			st=DB.connection.createStatement();
			rs=st.executeQuery("SELECT tag, tagid, paperkey, userid from tags where paperkey='"+DB.q(paperkey,16)+"' and isinvisible=false");
			// and isprivate=false 
			while(rs.next()){
					Tag tempTag=new Tag(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4));
				
				myTags.add(tempTag);
			}	
			myCollection=new TagBag(myTags);

		}catch (SQLException e){
			System.err.println("Err..."+e);
		}
		return myCollection;
	}

}
